package UI;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import DBTasks.BreedList;

@SuppressWarnings("serial")
public class SearchPanel extends JPanel implements ActionListener {

	private JTextField shelterZipInput, petZipInput;
	private JButton shelterSearchButton, petSearchButton, logoutButton;
	private JComboBox typeList, breedList, genderList, ageList;
	private MainFrame frame;
	private JComboBox[] boxes;

	public SearchPanel(MainFrame mainFrame) {
		
		this.setSize(500,600);
		this.frame = mainFrame;
		
		JLabel logo = new JLabel("APAC Search Page");
		logo.setFont(new Font("Tahoma", Font.BOLD, 30));
		
		JLabel shelterSearch = new JLabel("Search for Shelters");
		shelterSearch.setFont(new Font("Tahoma", Font.CENTER_BASELINE, 20));
		JLabel petSearch = new JLabel("Search for Pets");
		petSearch.setFont(new Font("Tahoma", Font.CENTER_BASELINE, 20));
		
		JLabel shelterZip = new JLabel("Enter Zip Code:");
		shelterZipInput = new JTextField(20);
		
		shelterSearchButton = new JButton("Search");
		shelterSearchButton.addActionListener(this);
		
		petSearchButton = new JButton("Search");
		petSearchButton.addActionListener(this);
		
		logoutButton = new JButton("Logout");
		logoutButton.addActionListener(this);
		
		JLabel petZip = new JLabel("Zip Code:");
		JLabel type = new JLabel("Type:");
		JLabel breed = new JLabel("Breed:");
		JLabel gender = new JLabel("Gender:");
		JLabel age = new JLabel("Age:");
		
		petZipInput = new JTextField(20);
		
		String[] types = {"Dog", "Cat"};
		typeList = new JComboBox(types);
		typeList.setSelectedIndex(0);
		typeList.addActionListener(this);
		
		boxes = BreedList.listBreeds();
	    
		breedList = new JComboBox();
		try {
			setComboBox("Dog");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		breedList.setSelectedIndex(0);
		
		String[] genders = {"M", "F"};
		genderList = new JComboBox(genders);
		genderList.setSelectedIndex(0);
		
		String[] ages = {"Baby", "Young", "Middle Age", "Old"};
		ageList = new JComboBox(ages);
		ageList.setSelectedIndex(0);
		
		GroupLayout gl = new GroupLayout(this);
		
		gl.setHorizontalGroup(gl.createParallelGroup(GroupLayout.Alignment.CENTER)
				.addComponent(logo)
				.addComponent(shelterSearch)
				.addGroup(gl.createSequentialGroup()
						.addGap(150)
						.addComponent(shelterZip)
						.addComponent(shelterZipInput)
						.addGap(150)
				)
				.addComponent(shelterSearchButton)
				.addComponent(petSearch)
				.addGroup(gl.createSequentialGroup()
						.addGap(100)
						.addGroup(gl.createParallelGroup(GroupLayout.Alignment.CENTER)
								.addComponent(petZip)
								.addComponent(type)
								.addComponent(breed)
								.addComponent(gender)
								.addComponent(age))
						.addGroup(gl.createParallelGroup(GroupLayout.Alignment.CENTER)
								.addComponent(petZipInput)
								.addComponent(typeList)
								.addComponent(breedList)
								.addComponent(genderList)
								.addComponent(ageList))
						.addGap(100)
								)
				.addComponent(petSearchButton)
				.addGroup(gl.createSequentialGroup()
						.addGap(400)
						.addComponent(logoutButton)));
		
		gl.linkSize(SwingConstants.VERTICAL, shelterZipInput, petZipInput, typeList, breedList, genderList, ageList);
		
		gl.setVerticalGroup(gl.createSequentialGroup()
	    		.addGap(15)
			    .addComponent(logo)
			    .addGap(50)
			    .addComponent(shelterSearch)
			    .addGap(15)
			    .addGroup(gl.createParallelGroup(GroupLayout.Alignment.CENTER)
						.addComponent(shelterZip)
						.addComponent(shelterZipInput)
				)
			    .addGap(15)
			    .addComponent(shelterSearchButton)
			    .addGap(30)
			    .addComponent(petSearch)
			    .addGap(15)
			    .addGroup(gl.createParallelGroup(GroupLayout.Alignment.LEADING)
			        .addGroup(gl.createSequentialGroup()
			        	.addGroup(gl.createParallelGroup(GroupLayout.Alignment.CENTER)
							 .addComponent(petZip)
							 .addComponent(petZipInput))
						.addGap(20)
						.addGroup(gl.createParallelGroup(GroupLayout.Alignment.CENTER)
					        .addComponent(type)
					        .addComponent(typeList))
					    .addGap(20)
			        	.addGroup(gl.createParallelGroup(GroupLayout.Alignment.CENTER)
					        .addComponent(breed)
					        .addComponent(breedList))
					    .addGap(20)
			            .addGroup(gl.createParallelGroup(GroupLayout.Alignment.CENTER)
			                .addComponent(gender)
			                .addComponent(genderList))
			            .addGap(20)
			            .addGroup(gl.createParallelGroup(GroupLayout.Alignment.CENTER)
			                .addComponent(age)
			                .addComponent(ageList)))
			     )
			     .addGap(15)
			     .addComponent(petSearchButton)
			     .addGap(20)
				 .addComponent(logoutButton)
				 .addGap(40)
	    );
		
		gl.setAutoCreateContainerGaps(true);
		gl.setAutoCreateGaps(true);
		
		this.setLayout(gl);
		
	}
	
	public void paintComponent(Graphics g) {
        super.paintComponents(g);
        g.setColor(Color.blue);
        g.fillRect(0, 0, this.getWidth(), 70);
    }

	@Override
	public void actionPerformed(ActionEvent e) {
		
		if(e.getSource().equals(shelterSearchButton)) {
			frame.setShelter(new ShelterResultsPanel(frame, Integer.parseInt(shelterZipInput.getText())));
			frame.setPanel(frame.getShelter());
        }
		
		else if(e.getSource().equals(typeList)) {
			JComboBox cb = (JComboBox)e.getSource();
	        String petType = (String)cb.getSelectedItem();
	        try {
				setComboBox(petType);
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
		
		else if(e.getSource().equals(petSearchButton)) {
			frame.setPet(new PetResultsPanel(frame, Integer.parseInt(petZipInput.getText()), (String) typeList.getSelectedItem(), 
					(String) breedList.getSelectedItem(), (String) genderList.getSelectedItem(), (String) ageList.getSelectedItem()));
			frame.setPanel(frame.getPet());
        }
		
		else if(e.getSource().equals(logoutButton)) {
			frame.setPanel(frame.getLogin());
        }
		
	}
	
	public void setComboBox(String type) throws SQLException {
		
		if(type == "Dog") {
			breedList.setModel(boxes[0].getModel());
		}
		
		if(type == "Cat") {
			breedList.setModel(boxes[1].getModel());
		}
		
		this.setSize(500, 600);
		
	}

}
